package com.hz.monitoring.module.hardware.convert.monitoring_data;

import java.util.*;
import java.util.stream.Collectors;

import com.hz.monitoring.framework.common.pojo.PageResult;


import com.hz.monitoring.framework.common.util.json.JsonUtils;
import com.hz.monitoring.module.hardware.controller.admin.vo.CustomSensorAttribute;
import com.hz.monitoring.module.hardware.controller.admin.vo.CustomValueFields;
import org.mapstruct.factory.Mappers;
import com.hz.monitoring.module.hardware.controller.admin.monitoring_data.vo.*;
import com.hz.monitoring.module.hardware.dal.dataobject.monitoring_data.MonitoringDataDO;
import org.springframework.util.StringUtils;

/**
 * 监测数据 Convert
 *
 * @author 芋道源码
 */
public class MonitoringDataConvert {

    
    public static MonitoringDataDO convert(MonitoringDataCreateReqVO bean) {
        if ( bean == null ) {
            return null;
        }
        String valueMetadata = null;
        if (Objects.nonNull(bean.getValueMetadata())) {
            valueMetadata = JsonUtils.toJsonString(bean.getValueMetadata());
        }

        MonitoringDataDO monitoringDataDO = new MonitoringDataDO();
        monitoringDataDO.setSensorTenantResourceId( bean.getSensorTenantResourceId() );
        monitoringDataDO.setMcuTenantResourceId( bean.getMcuTenantResourceId() );
        monitoringDataDO.setMcuModel( bean.getMcuModel() );
        monitoringDataDO.setSensorName( bean.getSensorName() );
        monitoringDataDO.setSensorModel( bean.getSensorModel() );
        monitoringDataDO.setValueMetadata(valueMetadata );
        monitoringDataDO.setTenantId(bean.getTenantId());

        return monitoringDataDO;
    }

    
    public static MonitoringDataDO convert(MonitoringDataUpdateReqVO bean) {
        if ( bean == null ) {
            return null;
        }
        String valueMetadata = null;
        if (Objects.nonNull(bean.getValueMetadata())) {
            valueMetadata = JsonUtils.toJsonString(bean.getValueMetadata());
        }
        MonitoringDataDO monitoringDataDO = new MonitoringDataDO();
        monitoringDataDO.setId( bean.getId() );
        monitoringDataDO.setSensorTenantResourceId( bean.getSensorTenantResourceId() );
        monitoringDataDO.setMcuTenantResourceId( bean.getMcuTenantResourceId() );
        monitoringDataDO.setMcuModel( bean.getMcuModel() );
        monitoringDataDO.setSensorName( bean.getSensorName() );
        monitoringDataDO.setSensorModel( bean.getSensorModel() );
        monitoringDataDO.setValueMetadata(valueMetadata);
        monitoringDataDO.setTenantId(bean.getTenantId());
        return monitoringDataDO;
    }

    
    public static MonitoringDataRespVO convert(MonitoringDataDO bean) {
        if ( bean == null ) {
            return null;
        }

        List<CustomValueFields> valueMetadata = new ArrayList<>();
        if (StringUtils.hasLength(bean.getValueMetadata())) {
            valueMetadata = JsonUtils.parseArray(bean.getValueMetadata(), CustomValueFields.class).stream().sorted(Comparator.comparing(CustomValueFields::getSorts)).collect(Collectors.toList());
        }
        MonitoringDataRespVO monitoringDataRespVO = new MonitoringDataRespVO();
        monitoringDataRespVO.setSensorTenantResourceId( bean.getSensorTenantResourceId() );
        monitoringDataRespVO.setMcuTenantResourceId( bean.getMcuTenantResourceId() );
        monitoringDataRespVO.setMcuModel( bean.getMcuModel() );
        monitoringDataRespVO.setSensorName( bean.getSensorName() );
        monitoringDataRespVO.setSensorModel( bean.getSensorModel() );
        monitoringDataRespVO.setValueMetadata(valueMetadata);
        monitoringDataRespVO.setId( bean.getId() );
        monitoringDataRespVO.setCreateTime( bean.getCreateTime() );
        monitoringDataRespVO.setTenantId(bean.getTenantId());

        return monitoringDataRespVO;
    }

    
    public static List<MonitoringDataRespVO> convertList(List<MonitoringDataDO> list) {
        if ( list == null ) {
            return null;
        }

        List<MonitoringDataRespVO> list1 = new ArrayList( list.size() );
        for ( MonitoringDataDO monitoringDataDO : list ) {
            list1.add( convert( monitoringDataDO ) );
        }

        return list1;
    }

    
    public static PageResult<MonitoringDataRespVO> convertPage(PageResult<MonitoringDataDO> page) {
        if ( page == null ) {
            return null;
        }

        PageResult<MonitoringDataRespVO> pageResult = new PageResult<MonitoringDataRespVO>();

        pageResult.setList( convertList( page.getList() ) );
        pageResult.setTotal( page.getTotal() );

        return pageResult;
    }

    
    public static List<MonitoringDataExcelVO> convertList02(List<MonitoringDataDO> list) {
        if ( list == null ) {
            return null;
        }

        List<MonitoringDataExcelVO> list1 = new ArrayList<MonitoringDataExcelVO>( list.size() );
        for ( MonitoringDataDO monitoringDataDO : list ) {
            list1.add( monitoringDataDOToMonitoringDataExcelVO( monitoringDataDO ) );
        }

        return list1;
    }

    public static MonitoringDataExcelVO monitoringDataDOToMonitoringDataExcelVO(MonitoringDataDO monitoringDataDO) {
        if ( monitoringDataDO == null ) {
            return null;
        }
        List<CustomValueFields> valueMetadataList = new ArrayList<>();
        if (StringUtils.hasLength(monitoringDataDO.getValueMetadata())) {
            valueMetadataList = JsonUtils.parseArray(monitoringDataDO.getValueMetadata(), CustomValueFields.class).stream().sorted(Comparator.comparing(CustomValueFields::getSorts)).collect(Collectors.toList());
        }
        String valueMetadata ="";
        for (CustomValueFields customValueFields:valueMetadataList){
            valueMetadata+=customValueFields.getName()+":"+customValueFields.getValue()+"   ";
        }

        MonitoringDataExcelVO monitoringDataExcelVO = new MonitoringDataExcelVO();

        monitoringDataExcelVO.setId( monitoringDataDO.getId() );
        monitoringDataExcelVO.setSensorTenantResourceId( monitoringDataDO.getSensorTenantResourceId() );
        monitoringDataExcelVO.setMcuTenantResourceId( monitoringDataDO.getMcuTenantResourceId() );
        monitoringDataExcelVO.setMcuModel( monitoringDataDO.getMcuModel() );
        monitoringDataExcelVO.setSensorName( monitoringDataDO.getSensorName() );
        monitoringDataExcelVO.setSensorModel( monitoringDataDO.getSensorModel() );
        monitoringDataExcelVO.setValueMetadata(valueMetadata);
        monitoringDataExcelVO.setCreateTime( monitoringDataDO.getCreateTime() );

        return monitoringDataExcelVO;
    }
}
